package com.yonyougov.devops.cruxcisupport.userserver;

import com.yonyougov.devops.cruxcisupport.user.UserEntity;
import com.yonyougov.devops.cruxcisupport.user.UserService;
import com.yonyougov.devops.cruxcisupport.userserver.dto.*;
import com.yonyougov.devops.cruxcisupport.userserver.vo.*;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.exception.BusinessException;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;
import java.util.List;

@Api(value = "用户服务器模块", tags = "用户服务器模块")
@RestController
@RequestMapping("/api/v2/userServer")
public class UserServerController extends CrudController<UserServerEntity, BigInteger> {

    private UserService userService;
    private UserServerService userServerService;


    public UserServerController(UserService userService, UserServerService userServerService) {
        this.userService = userService;
        this.userServerService = userServerService;
    }

    @Override
    public BaseService<UserServerEntity, BigInteger> getService() {
        return userServerService;
    }

    @ApiOperation(value = "前端:用户关联Server的列表展示", notes = "可选择根据用户id和Server ip分页模糊查询用户Server关联")
    @PostMapping(value = "/ur/page/like")
    public BaseResponseDto findUserServerPageLike(@RequestBody @Valid UserServerPageLikeRequestDto request) {
        UserServerPageLikeReqVo userServerPageLikeReqVo = new UserServerPageLikeReqVo();
        BeanUtils.copyProperties(request, userServerPageLikeReqVo);
        int totalCount = userServerService.countByUserServerLike(userServerPageLikeReqVo);
        List<UserServerPageLikeRespVo> datas = userServerService.findUserServerPageLike(userServerPageLikeReqVo);
        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", totalCount);
        result.put("data", datas);
        return result;
    }

    @ApiOperation(value = "前端:批量关联用户server调用", notes = "批量关联用户server")
    @PostMapping(value = "/ur/add/batch")
    public BaseResponseDto addBatchUserServer(@RequestBody @Valid UserServerAddBatchRequestDto request) {
        return userServerService.addPublicKey(request.getUserId(), request.getServerId());
    }

    @ApiOperation(value = "前端:批量删除用户server关联", notes = "批量删除用户server关联")
    @PostMapping(value = "/ur/delete/batch/ids")
    @SneakyThrows
    public BaseResponseDto DeleteBatchUserRoleByIds(@RequestBody @Valid UserServerDeleteBatchRequestDto request) {
        UserEntity userEntity = new UserEntity();
        userEntity.setId(request.getUserId());
        UserEntity user = userService.findBy(userEntity);
        userServerService.deleteBatchUserServerByIds(user, request.getIds());
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端服务器关联用户列表调用", notes = "分页查询服务器关联用户列表")
    @PostMapping(value = "/ur/user")
    public BaseResponseDto findUserLinkedServerPageLike(@RequestBody @Valid UserServerQueryRequestDto request) {
        ServerLinkedUserPageLikeReqVo serverLinkedUserPageLikeReqVo = new ServerLinkedUserPageLikeReqVo();
        BeanUtils.copyProperties(request, serverLinkedUserPageLikeReqVo);
        List<ServerLinkedUserPageLikeRespVo> datas = userServerService.findUserLinkedServerPageLike(serverLinkedUserPageLikeReqVo);
        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        int totalCount = userServerService.countByUserLinkedServer(serverLinkedUserPageLikeReqVo);
        result.put("totalCount", totalCount);
        result.put("data", datas);
        return result;
    }

    @ApiOperation(value = "前端服务器查询公钥调用", notes = "查询服务器公钥列表")
    @PostMapping(value = "/ur/pub/key")
    public BaseResponseDto findServerPubKey(@RequestBody @Valid UserServerQueryRequestDto request) {
        return userServerService.findServerPubKey(request.getServerId());
    }

    @ApiOperation(value = "前端服务器删除公钥调用", notes = "查删除服务器公钥")
    @PostMapping(value = "/ur/delete/pub/key")
    public BaseResponseDto deleteServerPubKey(@RequestBody @Valid UserServerDeletePubKeyRequestDto request) throws BusinessException {
        ServerDeletePubKeyReqVo pubKeyReqVo = new ServerDeletePubKeyReqVo();
        BeanUtils.copyProperties(request, pubKeyReqVo);
        userServerService.deleteServerPubKey(pubKeyReqVo);
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端服务器输出公钥调用", notes = "查输出服务器公钥")
    @PostMapping(value = "/ur/print/pub/key")
    public BaseResponseDto printServerPubKey() {
        userServerService.printPubKey();
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端server执行命令调用", notes = "server执行命令调用")
    @PostMapping(value = "/ur/server/command")
    @SneakyThrows
    public BaseResponseDto execCommand(@RequestBody @Valid UserServerCommandRequestDto request) {
        userServerService.execCommand(request.getServerId(), request.getCommand());
        return BaseResponseDto.getSuccessInstance();
    }

}
